const { contextBridge, ipcRenderer } = require('electron')
const { electronAPI } = require('@electron-toolkit/preload')

// Custom APIs for renderer
const api = {
  send: (channel, data) => {
    const validChannels = ['login'] // 添加其他允许的通道
    if (validChannels.includes(channel)) {
      ipcRenderer.send(channel, data)
    }
  },
  on: (channel, func) => {
    const validChannels = ['login-response'] // 添加其他允许的通道
    if (validChannels.includes(channel)) {
      // 移除旧的监听器，避免重复
      ipcRenderer.removeAllListeners(channel)
      // 添加新的监听器
      ipcRenderer.on(channel, (event, ...args) => func(...args))
    }
  },
  removeAllListeners: (channel) => {
    const validChannels = ['login-response'] // 添加其他允许的通道
    if (validChannels.includes(channel)) {
      ipcRenderer.removeAllListeners(channel)
    }
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', api)
    contextBridge.exposeInMainWorld('electronAPI', electronAPI)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = api
  window.electronAPI = electronAPI
} 